package com.srp.backend.controller;

import com.srp.backend.common.Result;
import com.srp.backend.dto.FileUploadResponse;
import com.srp.backend.service.FileUploadService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import jakarta.validation.constraints.NotNull;
import java.util.List;

/**
 * 文件上传控制器
 */
@RestController
@RequestMapping("/api/file")
@CrossOrigin(origins = "*")
public class FileUploadController {

    @Autowired
    private FileUploadService fileUploadService;

    /**
     * 单文件上传
     */
    @PostMapping("/upload")
    public Result<FileUploadResponse> uploadFile(
            @RequestParam("file") @NotNull MultipartFile file,
            @RequestParam(value = "description", required = false) String description) {
        try {
            FileUploadResponse response = fileUploadService.uploadFile(file, description);
            return Result.success(response);
        } catch (RuntimeException e) {
            return Result.error(e.getMessage());
        } catch (Exception e) {
            return Result.error("文件上传失败，请稍后重试");
        }
    }

    /**
     * 多文件上传
     */
    @PostMapping("/upload/batch")
    public Result<List<FileUploadResponse>> uploadFiles(
            @RequestParam("files") @NotNull MultipartFile[] files,
            @RequestParam(value = "description", required = false) String description) {
        try {
            List<FileUploadResponse> responses = fileUploadService.uploadFiles(files, description);
            return Result.success(responses);
        } catch (RuntimeException e) {
            return Result.error(e.getMessage());
        } catch (Exception e) {
            return Result.error("文件上传失败，请稍后重试");
        }
    }

    /**
     * 获取文件列表
     */
    @GetMapping("/list")
    public Result<List<FileUploadResponse>> getFileList(
            @RequestParam(value = "page", defaultValue = "1") Integer page,
            @RequestParam(value = "size", defaultValue = "10") Integer size) {
        try {
            List<FileUploadResponse> files = fileUploadService.getFileList(page, size);
            return Result.success(files);
        } catch (Exception e) {
            return Result.error("获取文件列表失败");
        }
    }

    /**
     * 删除文件
     */
    @DeleteMapping("/delete/{fileId}")
    public Result<String> deleteFile(@PathVariable Long fileId) {
        try {
            fileUploadService.deleteFile(fileId);
            return Result.success("文件删除成功");
        } catch (RuntimeException e) {
            return Result.error(e.getMessage());
        } catch (Exception e) {
            return Result.error("文件删除失败");
        }
    }

    /**
     * 获取文件信息
     */
    @GetMapping("/info/{fileId}")
    public Result<FileUploadResponse> getFileInfo(@PathVariable Long fileId) {
        try {
            FileUploadResponse fileInfo = fileUploadService.getFileInfo(fileId);
            return Result.success(fileInfo);
        } catch (RuntimeException e) {
            return Result.error(e.getMessage());
        } catch (Exception e) {
            return Result.error("获取文件信息失败");
        }
    }
}